﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.IO;

namespace _110ConsultingMVC.Controllers
{
    public class HomeController : Controller
    {

        private string[] files = new string[] { 
            "~/Content/docs/110 Consulting ECS.docx",
            "~/Content/docs/110 Consulting Microsoft Yahoo Search Alliance.docx",
            "~/Content/docs/110 Consulting TWIST.docx",
            "~/Content/docs/110 Consulting Custom Console Application.docx" 
        };



        public ActionResult Index()
        {
            XDocument feedXML = XDocument.Load("http://110consulting.wordpress.com/feed/");
            XNamespace nsContent = "http://purl.org/rss/1.0/modules/content/";
            //select out a collection of anonymous types from RSS feed       
            // add the second jobs feed.

            XDocument careersFeedXML = XDocument.Load(@"http://www.maxhire.net/cp/(A(DRtKWznfzQEkAAAAOWNhNTgwZTItNGYwMi00OWY4LWExMjUtYjE1ZTA1M2VmNGM3jd5TjbRW67rlub9igyoRUX6iSsg1)S(iw5a5n450vblgb55vpvwq555))/rssCurrentJobs.aspx?site=5F6D361D43515B7D561C2B&L=EN");
            List<_110ConsultingMVC.Models.Feed> m = new List<Models.Feed>();

            m.AddRange(from rss in feedXML.Descendants("item")
                                select new _110ConsultingMVC.Models.Feed
                                {
                                    Title = rss.Element("title").Value,
                                    Link = rss.Element("link").Value,
                                    Description = rss.Element("description").Value,
                                    Content = rss.Element(nsContent + "encoded").Value,
                                    Category = rss.Element("category").Value,
                                    Date = rss.Element("pubDate").Value,
                                    ID = rss.Element("guid").Value
                                });

            m.AddRange(from rss in careersFeedXML.Descendants("item")
                       select new _110ConsultingMVC.Models.Feed
                       {
                           Title = rss.Element("title").Value,
                           Link = rss.Element("link").Value,
                           Description = rss.Element("description").Value,
                           //Content = rss.Element(nsContent + "encoded").Value,
                           Category = "JOBS@110",
                           Date = rss.Element("pubDate").Value,
                           ID = rss.Element("guid").Value
                       });

            return View(m.AsEnumerable<_110ConsultingMVC.Models.Feed>());

        }

        public ActionResult error()
        {
            return View("error");
        }

        public ActionResult info(int id)
        {
            ViewBag.File = string.Format("~/home/info/{0}", id);

            var cookie = HttpContext.Request.Cookies["Visited"];

            if(cookie != null && cookie.Value == "1")
            {
                try
                {
                    var tokens = files[id].Split('/');
                    string filename = tokens.Last();


                    FileStream fs = new FileStream(HttpContext.Request.MapPath(files[id]), FileMode.Open, FileAccess.Read);
                    return File(fs, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", filename);

                }
                catch (Exception e) {
                     return thank_you(e);
                }
            }

            cookie = new HttpCookie("Visited", "1");
            cookie.Expires = DateTime.Now.AddYears(1);
            HttpContext.Response.Cookies.Add(cookie);
            
            return View("info");
        }

        public ActionResult thank_you(Exception e)
        {
            ViewBag.Error = e.ToString();
            return View("thank_you");
        }

    }
}
